/*
 * The BlueCove driven Server that serves the Client 
*/

package com.costeaalex.blueserver.bluecove;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.bluetooth.*;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

import com.costeaalex.blueserver.communication.CommunicationManager;
import com.intel.bluetooth.BlueCoveImpl;

public class BlueCoveServer implements CommunicationManager
	{

	private LocalDevice local;
	private StreamConnectionNotifier notifier;
	private StreamConnection conn;
	private InputStream inStream;
	private OutputStream outStream;
	//private int outputBufferSize=4096;
	private int outputBufferSize=1024;
	//private ServiceRecord sr;
	
	public void start()
		{
		try
			{
			if(LocalDevice.isPowerOn())
				{
				local = LocalDevice.getLocalDevice();
				
				if (!local.setDiscoverable(DiscoveryAgent.GIAC))
					{
					System.out.println("Failed to change to the " + "discoverable mode");
					}
		
		        UUID uuid = new UUID(0x1101);
		        String connectionString = "btspp://localhost:" + uuid +";authenticate=false;encrypt=false;name=BlueRemoteServer";        
		 
		        notifier = (StreamConnectionNotifier) Connector.open( connectionString, Connector.READ_WRITE );
		        
		        //sr = local.getRecord(notifier);
		        //System.out.println(sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT , false));
		        //System.out.println(connectionString);
		        //System.out.println(notifier.toString());
		        System.out.println("Server setup.");
				}
			}
		catch (Exception e)
			{
			e.printStackTrace();
			}
		
		try
			{
			System.out.println("Server accepting clients.");
			conn=notifier.acceptAndOpen();
			 
	        //RemoteDevice dev = RemoteDevice.getRemoteDevice(conn);
	        //System.out.println("Remote device address: "+dev.getBluetoothAddress());
	        //System.out.println("Remote device name: "+dev.getFriendlyName(true));
	 		
	        //get streams
	        inStream=conn.openInputStream();
	        outStream=conn.openOutputStream();
	        System.out.println("Server connected with client.");
			}
		catch(Exception e)
			{
			e.printStackTrace();
			}
		}
	
	public void send(int data)
		{
		try
			{
			outStream.write(data);
			outStream.flush();
			} 
		catch (IOException e)
			{
			e.printStackTrace();
			}
		//System.out.println("Sent byte");
		}
	
	public int receive()
		{
		try
			{
			return inStream.read();
			} 
		catch (IOException e)
			{
			e.printStackTrace();
			}
		return 0;
		}
	
	public int receiveBlock(byte[] received)
		{
		try
			{
			return inStream.read(received);
			} 
		catch (IOException e)
			{
			e.printStackTrace();
			return -1;
			}
		}
	
	public void stop()
		{
		try
			{
			conn.close();
			} 
		catch (IOException e)
			{
			e.printStackTrace();
			}
		BlueCoveImpl.shutdown();
		System.out.println("Stopped server");
		}

	public void send(byte[] data)
		{
		try
			{
			outStream.write(data);
			outStream.flush();
			}
		catch (IOException e)
			{
			e.printStackTrace();
			}
		}

	public int getOutputBufferSize()
		{
		return outputBufferSize;
		}
	}